﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using RestFullChatAJAX.Models;
using RestFullChatAJAX.Models.DAO;
using Newtonsoft.Json.Linq;

namespace RestFullChatAJAX.Controllers
{
    public class AccountController : ApiController
    {
        // GET api/Account/username
        public IEnumerable<Account> GetFriend(String id)
        {
            Account acc = AccountDAO.GetAccountByUsername(id);
            List<Account> lst = new List<Account>();
            if (acc != null)
            {
                if (!String.IsNullOrEmpty(acc.Friend))
                {
                    JArray arr = JArray.Parse(acc.Friend);
                    JArray arrTemp = new JArray();
                    foreach (var o in arr)
                    {
                        Account temp = new Account();
                        String username = o["Username"].ToString().Trim();
                        String state = o["State"].ToString().Trim();
                        if (state == "U")
                        {
                            int i = IndexManager.FindStoreIndex(id.Trim());
                            StateController.AddToStore(i, username, "requestF");
                            arrTemp.Add(o);
                            continue;
                        }
                        if (state == "N")
                            continue;
                        temp.Username = username;
                        //Online
                        int index = IndexManager.FindStoreIndex(username);
                        if (-1 != index)
                        {
                            temp.State = true;
                        }
                        lst.Add(temp);
                        arrTemp.Add(o);
                    }
                    String json = IndexManager.ListtoStringJSon("", arrTemp);
                    acc.Friend = json;
                    AccountDAO.UpdateAccount(acc.Username, acc);
                }
            }
            return lst;
        }

        // GET api/Account/username?abas
        public String GetValidAccount(String id, [FromUri]String Password)
        {
            Account acc = AccountDAO.GetAccountByUsername(id.Trim());
            if (acc != null && acc.PassWord.CompareTo(Password.Trim()) == 0)
            {
                //Login success
                if (-1 == IndexManager.FindStoreIndex(id.Trim()))
                {
                    IndexManager.Add(id.Trim());
                    MessageController.Add();
                    StateController.Add();
                    NotifyFriend(id, "Online");
                    return "Valid";
                }
                return "Used";
            }
            return "Invalid";
        }

        // Post api/Account/username
        public String PostLogout(String id)
        {
            //Login success
            int index = IndexManager.FindStoreIndex(id.Trim());
            if (-1 != index)
            {
                IndexManager.Remove(id.Trim());
                MessageController.Remove(index);
                StateController.Remove(index);
                NotifyFriend(id, "Offline");
                return "sc";
            }
            return "er";
        }

        // POST api/Account with content
        public String PostCreateAccount([FromBody]Account account)
        {
            if (account.Friend == String.Empty)
            {
                if (null != AccountDAO.GetAccountByUsername(account.Username))
                    return "DU";
                if (null != AccountDAO.GetAccountByEmail(account.Email))
                    return "DE";
                if (AccountDAO.CreateAccount(account))
                {
                    IndexManager.Add(account.Username.Trim());
                    MessageController.Add();
                    StateController.Add();
                    NotifyFriend(account.Username.Trim(), "Online");
                    return "SC";
                }
            }
            return "ER";
        }

        private void NotifyFriend(String username, String type)
        {
            Account acc = AccountDAO.GetAccountByUsername(username);
            if (acc != null)
            {
                if (!String.IsNullOrEmpty(acc.Friend))
                {
                    JArray arr = JArray.Parse(acc.Friend);
                    foreach (var o in arr)
                    {
                        //Online
                        int index = IndexManager.FindStoreIndex(o["Username"].ToString().Trim());
                        if (-1 != index)
                            StateController.AddToStore(index, username, type);
                    }
                }
            }
        }
    }
}
